<?php
/**
 * Created by PhpStorm.
 * UserController: 86158
 * Date: 2020/8/1
 * Time: 14:06
 */

namespace app\merchant\controller\shop;

use app\http\validates\user\RegisterValidates;
use app\merchant\controller\AuthController;
use app\models\shop\MerchantAdmin;
use app\models\user\UserToken;
use app\Request;
use crmeb\services\UtilService;
use crmeb\traits\JwtAuthModelTrait;
use think\exception\ValidateException;

class User
{
    use JwtAuthModelTrait;
    /**
     * 注册
     * @param Request $request
     * @return mixed
     */
    public function register(Request $request)
    {
        list($account, $password, $captcha) = UtilService::postMore([['account', ''], ['password', ''], ['captcha', '654321']], $request, true);
        try {
            validate(RegisterValidates::class)->scene('register')->check(['account' => $account, 'password' => $password ,'captcha' => $captcha,]);
        } catch (ValidateException $e) {
            return app('json')->fail($e->getError());
        }
        if (strlen(trim($password)) < 6 || strlen(trim($password)) > 16)
            return app('json')->fail('密码必须是在6到16位之间');
        if ($password == '123456') return app('json')->fail('密码太过简单，请输入较为复杂的密码');
        $registerStatus = MerchantAdmin::register($account, $password);
        if ($registerStatus) return app('json')->success('注册成功');
        return app('json')->fail(MerchantAdmin::getErrorInfo('注册失败'));
    }

    /**
     * 登录
     * @param Request $request
     * @return mixed
     */
    public function login(Request $request)
    {
        $user = MerchantAdmin::where('account', $request->param('account'))->find();
        if ($user) {
            if ($user->pwd !== md5($request->param('password')))
                return app('json')->fail('账号或密码错误');
            if ($user->pwd === md5(123456))
                return app('json')->fail('请修改您的初始密码，再尝试登陆！');
        } else {
            return app('json')->fail('账号或密码错误');
        }

        $token = AuthController::getToken($user);

        if ($token) {
//            event('UserLogin', [$user, $token]);
            return app('json')->success('登录成功', ['token' => $token['token'], 'info' => $user]);
        } else
            return app('json')->fail('登录失败');
    }

    /***
     * 获取用户信息
     * @param Request $request
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function userInfo(Request $request)
    {
        list($uid) = UtilService::postMore([['uid', '']], $request, true);
        $user = MerchantAdmin::where('id', $uid)->find();
        if($user){
            return app('json')->success($user->toArray());
        }else{
            return app('json')->fail('获取信息失败');
        }
    }

    /**
     * 修改密码
     * @param Request $request
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function changePwd(Request $request)
    {
        $list = UtilService::postMore([
            ['uid', ''],
            ['pwd', ''],
            ['password', '']
        ], $request);
        $user = MerchantAdmin::where('uid', $list['id'])->find();
        if(!$user){
            return app('json')->fail('账号不存在');
        }
        if(md5($list['pwd']) != $user['pwd']){
            return app('json')->fail('原始密码错误');
        }
        if (strlen(trim($list['password'])) < 6 || strlen(trim($list['password'])) > 16)
            return app('json')->fail('密码必须是在6到16位之间');
        if ($list['password'] == '123456') return app('json')->fail('密码太过简单，请输入较为复杂的密码');
        $res = MerchantAdmin::where('id',$list['id'])->update(['pwd'=>md5($list['password'])]);
        if($res){
            return app('json')->success();
        }else{
            return app('json')->fail('修改密码失败');
        }
    }
}